﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RAP.Framework.Libary.DataSource.Context;

namespace RAP.Framework.Libary.SqlTaskParser
{
	/// <summary>
	/// 数据库查询任务
	/// </summary>
	public class SqlTask : IDisposable
	{
		/// <summary>
		/// 任务执行完成的回调
		/// </summary>
		protected IList<Action<SqlTask, SqlParseResult, IDataServiceContext>> _onSuccessCallbackQueue { get; set; }

		/// <summary>
		/// 任务执行失败的回调
		/// </summary>
		protected IList<Action<SqlTask, SqlParseResult, IDataServiceContext, Exception>> _onErrorCallbackQueue { get; set; }

		public SqlTask( ) { }

		public SqlTask( string sql )
		{
			if ( sql != null )
			{
				Sql.Append( sql );
			}
		}

		/// <summary>
		/// 注册任务执行完成的回调
		/// </summary>
		/// <param name="callback"></param>
		public virtual void OnSuccess( Action<SqlTask, SqlParseResult, IDataServiceContext> callback )
		{
			if ( _onSuccessCallbackQueue == null )
			{
				_onSuccessCallbackQueue = new List<Action<SqlTask, SqlParseResult, IDataServiceContext>>();
			}
			_onSuccessCallbackQueue.Add( callback );
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sqlParseResult"></param>
		/// <param name="context"></param>
		public virtual void Success( SqlParseResult sqlParseResult, IDataServiceContext context )
		{
			_onSuccessCallbackQueue?.All( callback =>
			{
				callback( this, sqlParseResult, context );
				return true;
			} );
		}

		/// <summary>
		/// 注册任务执行失败的回调
		/// </summary>
		/// <param name="callback"></param>
		public virtual void OnError( Action<SqlTask, SqlParseResult, IDataServiceContext, Exception> callback )
		{
			if ( _onErrorCallbackQueue == null )
			{
				_onErrorCallbackQueue = new List<Action<SqlTask, SqlParseResult, IDataServiceContext, Exception>>();
			}
			_onErrorCallbackQueue.Add( callback );
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sqlParseResult"></param>
		/// <param name="context"></param>
		/// <param name="ex"></param>
		public virtual void Error( SqlParseResult sqlParseResult, IDataServiceContext context, Exception ex )
		{
			_onErrorCallbackQueue?.All( callback =>
			{
				callback( this, sqlParseResult, context, ex );
				return true;
			} );
		}

		/// <summary>
		/// 释放资源
		/// </summary>
		public void Dispose( )
		{
			_onSuccessCallbackQueue?.Clear();
			_onErrorCallbackQueue?.Clear();

			_onSuccessCallbackQueue = null;
			_onErrorCallbackQueue = null;
			Reuslt = null;
		}

		/// <summary>
		/// 任务Id
		/// </summary>
		public string Id { get; set; }

		/// <summary>
		/// 数据源Id
		/// </summary>
		public string DataSourceId { get; set; }

		/// <summary>
		/// Sql语句
		/// </summary>
		public StringBuilder Sql { get; set; } = new StringBuilder();

		/// <summary>
		/// 本次查询是否开启事务，如果为空，则使用全局设置
		/// </summary>
		public bool? Trans { get; set; } = false;

		/// <summary>
		/// 本次查询是否需要进行DSql解析
		/// </summary>
		//public bool? NeedDSqlParse { get; set; } = true;

		/// <summary>
		/// 结果集别名集合
		/// </summary>
		public IList<string> ResultAlias { get; set; }

		/// <summary>
		/// 当前Task的查询结果集
		/// </summary>
		public IDictionary Reuslt { get; set; } = new Hashtable();

		/// <summary>
		/// 是否执行成功
		/// </summary>
		public bool Successed { get; set; }
	}
}
